////////////////////////////////////////////////////////////////////////////////////////////////////
// unshadowed, untextured, skinned phong for up to N positional/directional light sources
////////////////////////////////////////////////////////////////////////////////////////////////////

const int num_lights = 1;

attribute vec3	Vertex;
attribute vec3	Normal;
attribute vec4	Weight;

uniform mat4	mvp;
uniform mat4	bone[27]; // maximum 64 addressable
uniform vec4	lp_obj[num_lights];
uniform vec4	vp_obj;

varying vec3	n_obj_i;
varying vec3	l_obj_i[num_lights];
varying vec3	h_obj_i[num_lights];

void main()
{
	vec3 Pobj	= Vertex;
	vec3 Nobj	= Normal;
	vec4 blend	= Weight;

	vec4 weight = vec4(blend.xyz, 1.0 - (blend.x + blend.y + blend.z));

	vec4 index = blend.w * vec4(1.0, 1.0 / 64.0, 1.0 / 4096.0, 1.0 / 262144.0);
	index = mod(index, vec4(64.0));

	vec3 p_obj = (bone[int(index[0])] * vec4(Pobj, 1.0)).xyz * weight[0];
#if 0
	vec3 n_obj = (mat3(bone[int(index[0])]) * Nobj) * weight[0];
#else
	vec3 n_obj = (mat3(bone[int(index[0])][0].xyz,
					   bone[int(index[0])][1].xyz,
					   bone[int(index[0])][2].xyz) * Nobj) * weight[0];
#endif

	for (int i = 1; i < 4; ++i)
	{
		p_obj += (bone[int(index[i])] * vec4(Pobj, 1.0)).xyz * weight[i];
#if 0
		n_obj += (mat3(bone[int(index[i])]) * Nobj) * weight[i];
#else
		n_obj += (mat3(bone[int(index[i])][0].xyz,
					   bone[int(index[i])][1].xyz,
					   bone[int(index[i])][2].xyz) * Nobj) * weight[i];
#endif
	}

	gl_Position = mvp * vec4(p_obj, 1.0);
	n_obj_i = normalize(n_obj);

	vec3 v_obj = normalize(vp_obj.xyz - p_obj * vp_obj.w);

	for (int i = 0; i < num_lights; ++i)
	{
		vec3 l_obj = normalize(lp_obj[i].xyz - p_obj * lp_obj[i].w);

		l_obj_i[i] = l_obj;
		h_obj_i[i] = l_obj + v_obj;
	}
}
